⚡ Bolt: Replace O(n) backtest array filtering with O(log n) binary search#63
⚡ Bolt: Replace O(n) backtest array filtering with O(log n) binary search#63toreleon wants to merge 2 commits into
Conversation
…st lookups Replaces O(n) `Array.prototype.filter()` array allocations with an O(log n) binary search utility `findLastBarIndex` when looking up historical time series data in the hot loop of the backtest runner. Co-authored-by: toreleon <42534763+toreleon@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
…st lookups Replaces O(n) `Array.prototype.filter()` array allocations with an O(log n) binary search utility `findLastBarIndex` when looking up historical time series data in the hot loop of the backtest runner. Also includes package upgrades for `undici` and `ws` to resolve high-severity vulnerability audit failures. Co-authored-by: toreleon <42534763+toreleon@users.noreply.github.com>
💡 What:
Added a binary search utility function
findLastBarIndexand replaced occurrences ofArray.prototype.filter(b => b.time <= asOf)with it when querying historical prices inside the backtest runner loop. Also updatedclipBarsto use.slice()combined with the binary search index.🎯 Why:
During backtesting simulations, the functions
priceOverrideandvnindexAtevaluate closures at every tick (interval turn) in a loop for multiple tickers. Previously, both were using an O(n)Array.prototype.filter()over long, chronologically-sorted price arrays simply to find the most recent bar prior toasOf. This constantly allocated large intermediate arrays and re-evaluated the entire dataset, severely degrading performance. Because market timeseries data is strictly chronologically ordered, we can leverage O(log n) binary search bounds-checking instead.📊 Impact:
Replaces an O(n) filter and array allocation with an O(log n) integer search and direct indexing lookup on hot paths. This should yield measurable CPU time improvements and reduced memory churn when backtesting across long date ranges with many 30m interval turns.
🔬 Measurement:
Run a heavy backtest session (
/backtestfrom the TUI spanning several months) and compare execution times or memory profiles. Alternatively, execute unit tests viapnpm testto verify correctness is preserved.PR created automatically by Jules for task 7119260057156216071 started by @toreleon